/*员工列表*/
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'fun/emp/list',
        datatype: "json",
        colModel: [
            { label: '员工ID', name: 'empId', index: "emp_id", width: 200, key: true},
            { label: '员工名', name: 'empName', sortable: false,width: 250 },
            { label: '身份证号', name: 'idCard',sortable: false, width: 100 },
            { label: '职名(务)', name: 'office', sortable: false, width: 150 },
            { label: '所属部门', name: 'deptName', sortable: false, width: 150 },
            { label: '岗位状态', name: 'status', sortable: false, width: 100 , formatter: function(value, options, row){
                    if(value === 0)
                        return '<span >在岗</span>'
                    else if(value === 5)
                        return '<span >不在岗</span>'
                }},
        ],
        viewrecords: true,
        height: 400,
        rowNum: 20,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth:25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            //$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            $("#jqGrid").setGridParam().hideCol("empId").trigger("reloadGrid");
        },
    });
});


var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            empName: null
        },
        showList: true,
        title: null,
        office: null,

        pickerOptions: {
            disabledDate(time) {
                return time.getTime() > Date.now();
            }
        },
        dialogFormVisible: false,
        dialogDeptVisible: false,
        dialog: false,
        drawer: false,
        formLabelWidth: '80px',
        timer: null,
        changeOfficeEmpIds: null,
        dict: [],
        timeout: null
    },

    methods: {
        query: function () {
            vm.reload();
        },
        handleCloseDrawer: function (done) {
            vm.dialog = false;
        },
        cancelForm: function () {
            vm.dialog = false;
        },
        handlePreview: function (file) {
            console.log(file);
        },
        handleRemove: function (file, fileList) {
            console.log(file, fileList);
        },
        fresh: function (event) {
            $("#jqGrid").trigger("reloadGrid");
            vm.dialogFormVisible = false;
        },
        freshDept: function (event) {
            $("#jqGrid").trigger("reloadGrid");
            vm.dialogDeptVisible = false;
        },
        switchOffice: function () {
            vm.changeOfficeEmpIds = getSelectedRows();
            if (vm.changeOfficeEmpIds == null) {
                return;
            }
            vm.dialog = true;
            vm.office = null;
            vm.getDict();
        },
        querySearchAsync: function (queryString, cb) {
            var dict = vm.dict;
            var results = queryString ? dict.filter(this.createStateFilter(queryString)) : dict;

            clearTimeout(this.timeout);
            this.timeout = setTimeout(() => {
                cb(results);
            }, 3000 * Math.random());
        },
        createStateFilter: function (queryString) {
            return (office) => {
                return (office.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
            };
        },
        handleSelect: function (item) {
            console.log(item);
            },
        getDict: function () {
            $.get(baseURL + "fun/emp/dictList", function (r) {
                vm.dict = r.dict;
            });
        },
        changeOffice: function () {
            if (vm.office == null) {
                alert('职名不能为空');
                return;
            }
            $.ajax({
                type: "POST",
                url: baseURL + "fun/emp/changeOffice",
                contentType: "application/json",
                data: JSON.stringify({'empIds': vm.changeOfficeEmpIds, 'office': vm.office}),
                success: function (r) {
                    if (r.code == 0) {
                        vm.dialog = false;
                        alert('操作成功', function (index) {
                            $("#jqGrid").trigger("reloadGrid");
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.empName, 'deptId': ''},
                page: page
            }).trigger("reloadGrid");
        },
        back: function (event) {
            history.go(-1);
        }
      }
});
window.addEventListener("message",function(event){
    vm.showList = true;
    var page = $("#jqGrid").jqGrid('getGridParam','page');
    $("#jqGrid").jqGrid('setGridParam',{
        postData:{'deptId':event.data,'name': ''},
        page:page
    }).trigger("reloadGrid");
}, false);